<?php

namespace app\admin\middleware;

use app\admin\model\Auth as ModelAuth;
use think\facade\Session;

class Auth
{
    public function handle($request, \Closure $next)
    {
        $response = $next($request);
        $app = app('http')->getName();
        $controller = strtolower($request->controller());
        $action = strtolower($request->action());
        $url = '/' . $app . '/' . $controller . '/' . $action;
        if (Session::has('admin')) {
            $permitUrl = ModelAuth::get(Session::get('admin.user_id'));
            // 当返回为数组时 而不是all则不是站长
            // dump($permitUrl , $url);die;
            // 公共控制器
            $publiCon = ['index', 'login', 'account'];
            if (!in_array($controller, $publiCon)) {
                if (is_array($permitUrl) && $permitUrl != 'all') {
                    if (!in_array($url, $permitUrl)) {
                        return view('tip/auth', ['msg' => '权限不足']);
                    }
                }
            }
        }

        return $response;
    }
}
